<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicModal
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'maintenance:management:inMaintenanceInfo:edit'"
    @register="registerTaskFormModal"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup name="ViewsMaintenanceManagementInMaintenanceInfoForm">
  import { ref, unref, computed, defineEmits,defineProps,watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { InMaintenanceInfo, inMaintenanceInfoSave, inMaintenanceInfoForm } from '/@/api/maintenance.management/inMaintenanceInfo';
  import { officeTreeData } from '/@/api/sys/office';


  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('maintenance.management.inMaintenanceInfo');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<InMaintenanceInfo>({} as InMaintenanceInfo);
  

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增维保任务信息表') : t('编辑维保任务信息表'),
  }));

// 定义props
const props = defineProps({
    record: {
      type: Object,
      default: () => ({})
    }
  });

  console.log('props', props);

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('任务标题'),
      field: 'title',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        // disabled:true
      },
    },
    {
      label: t('执行开始时间'),
      field: 'startExecuteTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        // showTime: { format: 'HH:mm' },
        // disabled:true
      },
    },
    {
      label: t('任务完成时间'),
      field: 'completeTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        // showTime: { format: 'HH:mm' },
        // disabled:true
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerTaskFormModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({ loading: true });
    await resetFields();
    const res = await inMaintenanceInfoForm(data);
    record.value = (res.inMaintenanceInfo || {}) as InMaintenanceInfo;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setModalProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      let data = await validate();
      data={
        ...data,
        enterpriseId:recordValue.value.aenterpriseId,
        enterpriseName:recordValue.value.aenterpriseName,
        maintenanceEnterpriseId:recordValue.value.benterpriseId,
        maintenanceEnterpriseName:recordValue.value.benterpriseName,
        taskStartDate:recordValue.value.startDate,
        taskEndDate:recordValue.value.endDate,
        
      }
      setModalProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
      };
      // console.log('submit', params, data, record);
      const res = await inMaintenanceInfoSave(params, data);
      showMessage(res.message);
      setTimeout(closeModal);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
    // 定义一个响应式变量来存储record
const recordValue = ref(props.record);
// 监听props.record的变化
watch(() => props.record, (newValue) => {
  recordValue.value = newValue;
  console.log('record变化了，新值为:', newValue);

});

</script>
